<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="./css/bootstrap-v4.6.0.css" />
  <link rel="stylesheet" href="./css/login.css" />
</head>

<body>
  <div class="login-box">
    <div class="form-group">
      <label for="username">Account</label>
      <!-- 账号 -->
      <input type="text" class="form-control" id="username" autocomplete="off" />
      <small id="emailHelp" class="form-text text-muted">The available account is <strong>admin</strong></small>
    </div>
    <div class="form-group">
      <!-- 密码 -->
      <label for="password">Password</label>
      <input type="password" class="form-control" id="password" />
      <small id="emailHelp" class="form-text text-muted">The available password is <strong>123456</strong></small>
    </div>
    <button type="submit" class="btn btn-primary" id="btnLogin">
      Submit
    </button>
  </div>

  <script src="./lib/axios.js"></script>

  <script>
    // 请求方式 POST
    // 地址 http://ajax-api.itheima.net/api/login
    // 参数:  username 用户名     password  密码  ==> 请求体参数，写在data中

    // 步骤
    //  1. 给按钮注册click
    //  2. 获取输入框的值 ==> value属性
    //  3. 使用axios发送请求，之后根据响应结果，做提示
    const username = document.querySelector('#username')
    const password = document.querySelector('#password')
    const btn = document.querySelector('.btn')
    btn.addEventListener('click', async () => {
      try {
        const uname = username.value
        const word = password.value
        const { data: res } = await axios.post('http://ajax-api.itheima.net/api/login', { username: uname, password: word })
        // 错误写法：加密
        // axios.post('http://ajax-api.itheima.net/api/login?username=uname&password=word')

        // console.log(res);
        alert(res.message + ',' + '用户' + res.data.account)
      } catch (error) {
        console.clear()
        alert('没有注册请先注册')
      }




    })
  </script>
</body>

</html>